package jimm;

import java.util.*;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Font;
import javax.microedition.rms.RecordStore;
import jimm.util.ResourceBundle;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

import DrawControls.ListItem;
import DrawControls.VirtualList;
import DrawControls.TextList;
import DrawControls.VirtualListCommands;

import jimm.comm.Icq;
import jimm.comm.SearchAction;
import jimm.comm.Action;


import jimm.comm.Util;
import jimm.Search;
//#sijapp cond.if target="SIEMENS2" | target="MOTOROLA" | target="MIDP2"#
import javax.microedition.io.Connector;
//#sijapp cond.if target is "SIEMENS2"#
import com.siemens.mp.io.file.FileConnection;
import com.siemens.mp.io.file.FileSystemRegistry;
//#sijapp cond.else#
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
//#sijapp cond.end#
import jimm.ContactListContactItem;
//#sijapp cond.end#
// Class to cache one line in messages list
// All fields are public to easy and fast access




// Visual messages history list
class SpyList extends VirtualList
                         implements CommandListener, VirtualListCommands
		//#sijapp cond.if target="SIEMENS2" | target="MOTOROLA" | target="MIDP2"#
		, Runnable, FileBrowserListener
		//#sijapp cond.end#
{

	// commands for message text
	//#sijapp cond.if target is "MIDP2"#
	public static Command cmdMsgBack     = new Command(ResourceBundle.getString("back"),      Jimm.is_smart_SE() ? Command.CANCEL : Command.BACK, 1);
	//#sijapp cond.else#
	public static Command cmdMsgBack     = new Command(ResourceBundle.getString("back"),      Command.BACK, 1);
	//#sijapp cond.end#
	public static Command cmdMsgNext     = new Command(ResourceBundle.getString("next"),      Command.ITEM, 2);
	public static Command cmdMsgPrev     = new Command(ResourceBundle.getString("prev"),      Command.ITEM, 3);
	public static Command cmdMsgCopyText = new Command(ResourceBundle.getString("copy_text"), Command.ITEM, 4);
	
	// commands for messages list
	public static Command cmdSelect   = new Command(ResourceBundle.getString("select"),       Command.OK,   1);
	//#sijapp cond.if target is "MIDP2"#
	public static Command cmdBack     = new Command(ResourceBundle.getString("back"),         Jimm.is_smart_SE() ? Command.CANCEL : Command.BACK, 9);
	//#sijapp cond.else#
	public static Command cmdBack     = new Command(ResourceBundle.getString("back"),         Command.BACK, 9);
	//#sijapp cond.end#
	public static Command cmdClear    = new Command(ResourceBundle.getString("clear", ResourceBundle.FLAG_ELLIPSIS), Command.ITEM, 8); 
	public static Command cmdInfo     = new Command(ResourceBundle.getString("history_info"), Command.ITEM, 5);
	public static Command cmdCopytext = new Command(ResourceBundle.getString("copy_text"),    Command.ITEM, 4);
	//commands for url list
	public static Command cmdurlSelect   = new Command(ResourceBundle.getString("select"),    Command.OK,   1);
	//#sijapp cond.if target is "MIDP2"#
	public static Command cmdurlBack     = new Command(ResourceBundle.getString("back"),      Jimm.is_smart_SE() ? Command.CANCEL : Command.BACK, 2);
	//#sijapp cond.else#
	public static Command cmdurlBack     = new Command(ResourceBundle.getString("back"),      Command.BACK, 2);
	//#sijapp cond.end#

	
	static TextList messText;
	
	// list UIN
	private static String currUin  = new String(),
	                      currName = new String();
	
	// Controls for finding text
	private static Form frmFind;
	private static Command cmdFindOk;
	private static Command cmdFindCancel;
	private static TextField tfldFind;
	private static ChoiceGroup chsFind;

	public static Displayable backplace;

	
	// Constructor
	public SpyList()
	{
		super(null);
		
		leftCaption = ResourceBundle.getString("options_str");
		rightCaption = ResourceBundle.getString("back");

		setVLCommands(this);
		JimmUI.setColorScheme(this);
		setFullScreenMode(true);
	}
	
	// VirtualList command impl.
	public void onCursorMove(VirtualList sender)
	{
	}
	
	// VirtualList command impl.
	public boolean onKeyPress(VirtualList sender, int keyCode, int type)
	{
		if (type != VirtualList.KEY_PRESSED)
			return false;

		if (sender == messText)
		{
			switch ( getGameAction(keyCode) )
			{
			case Canvas.LEFT: moveInList(-1); return true; 
			case Canvas.RIGHT: moveInList(1); return true;
			}
		}


		if (keyCode == Jimm.leftSoftKey) 
		{
			Jimm.pum.showPopupInSpy(currUin, this);
			return true;
		}
		else if ((keyCode == Jimm.rightSoftKey) || ((Jimm.is_phone_SE()) && (keyCode == -11)) )
		{
			processCommand(CMD_BACK, null);
			return true;
		}


                return JimmUI.execHotKey(null, keyCode, type);
	}

	
	// Moves on next/previous message in list and shows message text
	private void moveInList(int offset)
	{
		moveCursor(offset, false);
		showMessText();
	}
	
	// VirtualList command impl.
	public void onItemSelected(VirtualList sender)
	{

		if (sender == this)
		{
			showMessText();
		}

	}
	
	//#sijapp cond.if target="MOTOROLA"|target="MIDP2"|target="SIEMENS2"#
	private boolean cp1251;

	private String exportUin;

	private String directory;

	//additional code -  nedeed beacause of interface FileBrowser
	public ContactListContactItem getCItem()
	{
		return null;
	}

	public void export(String uin)
	{
		exportUin = uin;
		try
		{
			FileBrowser.setListener(this);
			FileBrowser.setParameters(true);
			FileBrowser.activate();
		}
		catch (JimmException e)
		{
			JimmException.handleException(e);
		}
	}

	public void onFileSelect(String s0)
	{
	}

	public void onDirectorySelect(String dir)
	{
		directory = dir;
		(new Thread(this)).start();
	}

	public void run()
	{
//		if (exportUin == null) startExport(null);
//		else startExport(new ContactListContactItem[] { ContactList.getItembyUIN(exportUin) });
	}


      	static public final int CMD_ON		= 1;
	static public final int CMD_OFF		= 2;
	static public final int CMD_CLEAR	= 3;
	static public final int CMD_COPY	= 4;
	static public final int CMD_COPY_ALL	= 5;
	static public final int CMD_BACK	= 6;



	public void processCommand(int cmd, Displayable backscr)
	{
		switch (cmd)
		{
		case CMD_COPY:
		case CMD_COPY_ALL:
		{

			String str = "";
			ListItem itm = new ListItem();

			
			if (cmd == CMD_COPY_ALL)	
			{
			

			for (int i = 0; i < getSize(); i++)
			{
				get(i, itm);
				str += itm.text + "\n";
			}
			}
			else
			{
				get(getCurrIndex(), itm);
				str = itm.text;
			}

			JimmUI.setClipBoardText
			(
				"["+this.getCaption()+"]\n"
				+ str
			);
			break;  
	
		}

		case CMD_BACK:
		{
			Spy.clearCache();
			messText = null;
			Jimm.pum.destroyPopupInSpy();

			Jimm.display.setCurrent(backplace);

			return;
		}
		case CMD_ON:
		{
//			Options.addUinToSpied(currUin);
			Options.spiedUins.put(currUin, "1");
			Options.safe_save();
			break;
		}
		case CMD_OFF:
		{
//			Options.removeUinFromSpied(currUin);
			Options.spiedUins.remove(currUin);
			Options.safe_save();
			break;
		}

		case CMD_CLEAR:
		{
       		Spy.clear(currUin);
			messText = null;
			System.gc();
			break;
			
			//Jimm.gm.showMainMenu();
			//return;
		}
		}
		Jimm.display.setCurrent(this);
	}


	public void commandAction(Command c, Displayable d)
	{
/*
		// back to contact list
		if (c == cmdBack)
		{
		
			Spy.clearCache();
			messText = null;
			//System.gc();
			if (backplace == 0)
				Jimm.gm.showMainMenu(); //MainMenu.activate();
			else if (backplace == 1)
				ContactList.activate();
		}
		
		// select message
		else if (c == cmdSelect)
		{
			showMessText();
		}
		
		// Clear messages
		else if (c == cmdClear)
		{
			Spy.clear_all(null);
			messText = null;
			//System.gc();
			Jimm.gm.showMainMenu(); //MainMenu.activate();
		}
		
		// Copy text from messages list
		else if ((c == cmdCopytext) || (c == cmdMsgCopyText))
		{
			int index = getCurrIndex();
			if (index == -1) return;
			CachedRecord record = Spy.getCachedRecord(currUin, index);
			if (record == null) return;
			JimmUI.setClipBoardText
			(
				(record.type == 0),
				"", //record.date,
				record.from,
				record.text
			);
		}
		
		// back to messages list
		else if (c == cmdMsgBack)
		{
			Jimm.display.setCurrent(this);
		}
		
		// next message command
		else if (c == cmdMsgNext)
		{
			moveInList(1);
		}
		
		// previous message command 
		else if (c == cmdMsgPrev)
		{
			moveInList(-1);
		}
		// user select ok command in find screen
		else if (c == cmdFindOk)
		{
			Spy.find
			(
				currUin,
				tfldFind.getString(),
				chsFind.isSelected(1),
				chsFind.isSelected(0)
			);
		}
		
		else if (c == cmdFindCancel)
		{
			Jimm.display.setCurrent(this);
		}
		
		// commands info
		else if (c == cmdInfo)
		{
			Action act = null;

			CachedRecord record = Spy.getCachedRecord(currUin, getCurrIndex());

			Search search = new Search(true, true);
			String data[] = new String[Search.LAST_INDEX];
			data[Search.UIN] = record.from;
			act = new SearchAction(search, data, SearchAction.CALLED_BY_SEARCHUSER);

			try
			{
				Icq.requestAction(act);
			}
			catch (JimmException e)
			{
				JimmException.handleException(e);
				if (e.isCritical()) return;
			}
			// Start timer
			SplashCanvas.addTimerTask("wait", act, false);

		}
*/
	} // end 'commandAction'
	
	// Show text message of current message of messages list
	public void showMessText()
	{
		if (this.getCurrIndex() >= this.getSize()) return;
		if (messText == null)
		{
			messText = new TextList(null);
			messText.setCursorMode(TextList.SEL_NONE);
			messText.returnable = this;
			messText.setFullScreenMode(true);
			

//			messText.setCommandListener(this);
//			messText.addCommand(cmdMsgBack);
//			messText.addCommand(cmdMsgNext);
//			messText.addCommand(cmdMsgPrev);
//			messText.addCommand(cmdInfo);
//			messText.addCommand(cmdMsgCopyText);
			//messText.setVLCommands(this);
			JimmUI.setColorScheme(messText);
		}

		CachedRecord record = Spy.getRecord(currUin, this.getCurrIndex()); 

		messText.clear();
//		messText.addBigText(record.date+":", messText.getTextColor(), Font.STYLE_BOLD, -1);
//		messText.doCRLF(-1);
		
		//sijapp cond.if modules_SMILES is "true" #
//		Emotions.addTextWithEmotions(messText, record.text, Font.STYLE_PLAIN, messText.getTextColor(), -1);
		//sijapp cond.else#
		messText.addBigText(record.text, messText.getTextColor(), Font.STYLE_PLAIN, -1);
		//sijapp cond.end#
		
		
		messText.doCRLF(-1);

		ContactListContactItem itm = ContactList.getItembyUIN(record.from);
		if (itm != null)
		{
			messText.setCaption(itm.getStringValue(ContactListContactItem.CONTACTITEM_NAME));
		}

		//messText.returnable = Spy.getList();
	
		Jimm.display.setCurrent(messText);
//		messText.repaint();
	}
	
	// returns UIN of list
	static String getCurrUin()
	{
		return currUin;
	}
	
	// sets UIN for list
	static void setCurrUin(String currUin_, String currName_)
	{
		currUin  = currUin_;
		currName = currName_;
	}
	
	// returns size of messages history list
	protected int getSize()
	{
		return Spy.getRecordCount(currUin);
	}
	  
	// returns messages history list item data
	protected void get(int index, ListItem item)
	{
		CachedRecord record = Spy.getCachedRecord(currUin, index);

		if (record == null) return;
		item.text = record.shortText;
		item.color = (index % 2 == 0) ? getTextColor() : Options.getSchemeColor(Options.CLRSCHHEME_BLUE);
	}
}

// History storage implementation
public class Spy
{

	static final public int CLEAR_EACH_DAY   = 0;
	static final public int CLEAR_EACH_WEEK  = 1;
	static final public int CLEAR_EACH_MONTH = 2;
	
	static final private int VERSION = 1;

	private static RecordStore recordStore;
	private static SpyList list;
	private static String currCacheUin = new String();
	private static Hashtable cachedRecords;
	
	
	final static private int TEXT_START_INDEX = 0;

	public static SpyList getList()
	{
		return list;
	}

	public static void activate(Displayable backplace)
	{
		list.backplace = backplace;
		list.showMessText();
	}

	public static void processListCommand(int cmd, Displayable d)
	{
		list.processCommand(cmd, d);
	}
	
	public Spy()
	{
	}
	

	// Add message text to contact history
	static synchronized public void addText
	(
		String uin, 
		boolean onoff,
		boolean hidding,
		int status,
		int clientid,
		long time 
	)
	{
		if (!Options.spiedUins.containsKey(uin))//isUinSpied(uin))
		{
			return;
		}

		int[] loclaDate = Util.createDate(time);

		if  (status != -1)
		{
			status = status & 0x0000FFFF;
		}
//		System.out.print("Preparing status="); System.out.println(status);

		   CachedRecord cr = null;
			try 
			{

		//	openUINRecords(uin);
			int cnt = getRecordCount(uin);
			cr = getRecord(uin, cnt-1);
		//	recordStore.closeRecordStore();

			}
			catch (Exception e)
			{
			}

			if (cr != null)
			{
//				System.out.println(Integer.toString(cr.ttime));
//				System.out.println(Integer.toString(loclaDate[Util.TIME_HOUR]*100 + loclaDate[Util.TIME_MINUTE]));
			
			

			if ((onoff) && (status == cr.status) && (cr.onoff)&&(cr.ttime == loclaDate[Util.TIME_HOUR]*100 + loclaDate[Util.TIME_MINUTE]))
			{
//				System.out.println("going out...");
				return;
			}

			if ((status == cr.status)&&(cr.tdate == loclaDate[Util.TIME_MON]*100 + loclaDate[Util.TIME_DAY]))
			{
//				System.out.println("Equivalent statusez... exiting...");
				return;	
			}
			}
			


		if (JimmUI.getStatusString(status) == null)
			return;


		byte[] buffer, textData;
		int textLen;
		boolean lastLine = false;
		
		RecordStore recordStore = null;
		
		if (list != null)
		{
			if (list.getSize() == 0) lastLine = true;
			else if (list.getCurrIndex() == (list.getSize()-1)) lastLine = true;
		}
		
		try
		{
			recordStore = RecordStore.openRecordStore(getRSName(uin), true); 
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
			DataOutputStream das = new DataOutputStream(baos);
	
//			das.writeUTF(from);
//			das.writeUTF(text);

			
//			System.out.print("writing status=");
//			System.out.println(status);

			das.writeInt(status);
//			das.writeUTF(uin);
			if (hidding)
				das.writeByte(2);
			else
				das.writeByte(onoff? 1 : 0);
			das.writeByte(clientid);
			das.writeByte(loclaDate[Util.TIME_DAY]);
			das.writeByte(loclaDate[Util.TIME_MON]);
			das.writeShort(loclaDate[Util.TIME_YEAR]);
			das.writeByte(loclaDate[Util.TIME_HOUR]);
			das.writeByte(loclaDate[Util.TIME_MINUTE]);


//			das.writeUTF(Util.getDateString(false, time));
			textData = baos.toByteArray();
			textLen = textData.length;
			
//			buffer = new byte[textLen+TEXT_START_INDEX];
//			Util.putDWord(buffer, 0, recordStore.getNextRecordID());
//			buffer[0] = type;
//			System.arraycopy(textData, 0, buffer, TEXT_START_INDEX, textLen);
			
//			recordStore.addRecord(buffer, 0, buffer.length);
			recordStore.addRecord(textData, 0, textLen);
			
//			if (/*!isCurrenty && */ (recordStore != null))
			{
				recordStore.closeRecordStore();
				recordStore = null;
			}
		}
		catch (Exception e) 
		{
		}
		
		if ( (list != null))// && AllSeeingEyeList.getCurrUin().equals(uin) )
		{
			list.repaint();
			if (lastLine) list.setCurrentItem(list.getSize()-1);
		}
	}
	
	// Returns reference for record store
	static RecordStore getRS()
	{
		return recordStore;
	}
	
	// Returns record store name for UIN
	static private String getRSName(String uin)
	{
		return "spy" + uin;
	}
	
	// Opens record store for UIN
	static private void openUINRecords(String uin)
	{
//		if (currCacheUin.equals(uin)) return;
		
		try
		{
			if (recordStore != null)
			{
				recordStore.closeRecordStore();
				recordStore = null;
				System.gc();
			}
			recordStore = RecordStore.openRecordStore(getRSName(uin), true);
		}
		catch (Exception e)
		{
			recordStore = null;
			return;
		}
		currCacheUin = uin;
		
		if (cachedRecords == null) cachedRecords = new Hashtable();
	}
	
	// Returns record count for UIN
	static public int getRecordCount(String uin)
	{
//		openUINRecords(uin);
		int result;
		try
		{
			result = recordStore.getNumRecords();
		}
		catch (Exception e)
		{
			result = 0;			
		}
		
		return result; 
	}
	
	// Returns full data of stored message
	static synchronized public CachedRecord getRecord(String uin, int recNo)
	{
//		openUINRecords(uin);
		byte[] data;
		CachedRecord result = new CachedRecord();
		
		try
		{
			data = recordStore.getRecord(recNo+1);
			ByteArrayInputStream bais = new ByteArrayInputStream(data, 0, data.length);
			DataInputStream dis = new DataInputStream(bais);

			result.status = dis.readInt();

//			System.out.print("reading status=");
//			System.out.println(result.status);


			result.from = uin; //dis.readUTF();
			byte bb = dis.readByte();
			result.hidding = (bb == 2);
			result.onoff = (bb == 1);
			result.clientid = dis.readByte();
			int dday = dis.readByte();
			int dmon = dis.readByte();
			int dyear = dis.readShort();
			int dhour = dis.readByte();
			int dmin = dis.readByte();

			result.ttime = dhour*100 + dmin;
			result.tdate = dmon*100 + dday;


			String reasn; // = new String("unknown");
			String reasnl;// = new String("unknown event");
			
			if (!result.hidding)
			{
			reasn = JimmUI.getStatusString(result.status);
			reasnl = JimmUI.getStatusString(result.status);
			} else	
			{
				reasn = ResourceBundle.getString("eye_short_hidden");
				reasnl = reasn;
			}
			//System.out.println("status="+reasn);
	
/*

			if (result.hidding)
			{
				reasn = JimmUI.getStatusString(status); //ResourceBundle.getString("hidding");
				reasnl = ResourceBundle.getString("hidding");
			} 
			else
			if (result.onoff)
			{
				reasn = ResourceBundle.getString("zashel");
				reasnl = ResourceBundle.getString("zashel");
			}
			else
			{
				reasn = ResourceBundle.getString("vyshel");
				reasnl = ResourceBundle.getString("vyshel");
			}
*/
			ContactListContactItem clci = ContactList.getItembyUIN(result.from);
			String clciName = new String();
		
			if (clci != null)
			{
				clciName = clci.getStringValue(ContactListContactItem.CONTACTITEM_NAME);
				//result.text = result.from + " (" + clciName + ")";

				//result.shortText = clciName;
			}
			else
			{	
				//result.text = result.from;
			        //result.shortText = result.from;
			}

		
			String dots = ":";
			String space = " ";
			String dateonly = Util.makeTwo(dday) + "." + Util.makeTwo(dmon) + "."; // + String.valueOf(dyear);
			String dateonlyshort;
		
			if (SplashCanvas._this.getWidth() > 160)
				dateonlyshort = dateonly + Util.makeTwo(dyear%100);
			else
				dateonlyshort = Util.makeTwo(dday) + "." + Util.makeTwo(dmon);
				

			dateonly = dateonly + String.valueOf(dyear);

			String timeonly = Util.makeTwo(dhour) + dots + Util.makeTwo(dmin);


			result.text = dateonly  + " " + timeonly +  "\n" + 
					ResourceBundle.getString("status") + dots + space + reasnl + "\n" + 
					ResourceBundle.getString("client_id") + dots + space + Util.getClientString(result.clientid) ;
			                                                                     

			result.shortText = dateonlyshort + space + timeonly + "," + space + reasn;

		}
		catch (Exception e)
		{
			result.text = result.date = result.from = "error"; 
			//#sijapp cond.if target is "SIEMENS2" | target is "MIDP2"#
			result.contains_url = false;
			//#sijapp cond.end#
			return null;
		}
		
		return result;
	}               		
	
	// returns cached short text of the message for history list
	static public CachedRecord getCachedRecord(String uin, int recNo)
	{
		int maxLen = 20;
		CachedRecord cachedRec = (CachedRecord)cachedRecords.get(new Integer(recNo)); 
		if (cachedRec != null) return cachedRec;
		
		cachedRec = getRecord(uin, recNo);
		if (cachedRec == null) return null;
/*
		if (cachedRec.text.length() > maxLen) 
			cachedRec.shortText = cachedRec.text.substring(0, maxLen)+"...";
		else */
//			cachedRec.shortText = cachedRec.text;

		
        cachedRec.shortText = cachedRec.shortText.replace('\n', ' ');
        cachedRec.shortText = cachedRec.shortText.replace('\r', ' ');

//		cachedRec.shortText = "brbr";
        
		cachedRecords.put(new Integer(recNo), cachedRec);
		
		return cachedRec;
	}
	
	// Shows history list on mobile phone screen
	static public void showHistoryList(String uin, String nick, Displayable d)
	{
		list.backplace = d;

		openUINRecords(uin);
		          
//		if (list == null)
//		{
			String caption = ResourceBundle.getString("statistics");

			ContactListContactItem itm = ContactList.getItembyUIN(uin);

			if (itm != null)
			{
				caption = caption + " " + itm.getStringValue(ContactListContactItem.CONTACTITEM_NAME);
			}


			list = new SpyList();
			list.setCaption(caption);
//		}
		
		SpyList.setCurrUin(uin, nick);

		list.lock();
		
		if (list.getSize() != 0) list.setCurrentItem(list.getSize()-1);
		list.unlock();

		Jimm.display.setCurrent(list);
	}
	
	
	// Clears cache before hiding history list
	static public void clearCache()
	{
		if (cachedRecords != null)
		{
			cachedRecords.clear();
			cachedRecords = null;
		}
		
		list = null;
		
		currCacheUin = new String();
	}
	
	// Sets color scheme for history UI controls
	static public void setColorScheme()
	{
		if (list != null)
		{
			JimmUI.setColorScheme(list);
			if (SpyList.messText != null) 
				JimmUI.setColorScheme(SpyList.messText);
		}
	}
	
	static synchronized private boolean find_intern(String uin, String text, boolean case_sens, boolean back)
	{
		int index = list.getCurrIndex();
		if ((index < 0) || (index >= list.getSize())) return false;
		if (!case_sens) text = text.toLowerCase();
		int size = getRecordCount(uin);
		
		for (;;)
		{
			if ((index < 0) || (index >= size)) break;
			CachedRecord record = getRecord(uin, index);
			String search_text = 
				case_sens ? record.text : record.text.toLowerCase();
			if (search_text.indexOf(text) != -1)

			{
				list.setCurrentItem(index);
				Jimm.display.setCurrent(list);
				return true;
			}
			
			if (back) index--;
			else index++;
		}
		return false;
	}
	
	// find text
	static void find(String uin, String text, boolean case_sens, boolean back)
	{
		if (list == null) return;
		boolean result = find_intern(uin, text, case_sens, back);
		if (result == true) return;
		
		Alert alert = new Alert
		(
			ResourceBundle.getString("find"),
			(new StringBuffer())
				.append(text)
			    .append("\n")
				.append(ResourceBundle.getString("not_found"))
				.toString(),
			null,
			AlertType.INFO
		);
		
		alert.setTimeout(Alert.FOREVER);
		Jimm.display.setCurrent(alert, list);
	}
	
	static synchronized void clear(String uin)
	{
		try
		{
			recordStore.closeRecordStore();
			recordStore = null;
		
			RecordStore.deleteRecordStore(getRSName(uin));
			System.gc();
		}
		catch (Exception e)
		{
		}
	}

/*
	// Clears all records for all uins
	static synchronized void clear_all()
	{
		try
		{
			recordStore.closeRecordStore();
			recordStore = null;
		
			for (int i = 0; i < Options.getSpiedUinsCount(); i++)
			{
				RecordStore.deleteRecordStore(getRSName(Options.spiedUins[i]));
			}
			
			//System.gc();
			
		}
		catch (Exception e)
		{

		}
	}
*/
}

